<?php
class History_trouble extends arfaController{

	public function __construct()
	{
		parent::__construct();
		$this->load->model("m_history_trouble");
		$this->load->model("m_proses_tl_trouble");
		$this->load->library("Datatables");
		$this->load->library("table");
	}
	
	public function grid_history_trouble()
	{
		$data["data_history_trouble"]	= $this->m_history_trouble->get_data();
		$data["menu"] 					= "admin";
		$data["content"] 				= $this->load->view("v_grid_history_trouble",$data,true);
		$this->load->view("main",$data);
	}
	
	public function form_history_trouble($id = NULL)
	{
		$this->form_validation->set_rules("tanggal","Tanggal","required");
		$this->form_validation->set_rules("waktu","Waktu","required");
		$this->form_validation->set_rules("keterangan","Keterangan","required");
		$this->form_validation->set_rules("jenis_gangguan","Jenis Ganguan","required");

		if($this->form_validation->run() == FALSE)
		{
			if(!isset($id))
			{
				$data["menu"]			= "admin";
				$data["post_action"]	= "history_trouble/form_history_trouble";
				$data["content"]		= $this->load->view("form_history_trouble",$data,true);
				$this->load->view("main",$data);
			}
			else
			{
				$data["id"]				= $id;
				$data["list"]			= $this->m_history_trouble->get($id);
				$data["menu"]			= "admin";
				$data["post_action"]	= "history_trouble/form_history_trouble/".$id;
				$data["content"]		= $this->load->view("form_history_trouble",$data,true);
				$this->load->view("main",$data);
			}
		}
		else
		{
			$get_tanggal	= $this->input->post("tanggal");
			$get_waktu		= $this->input->post("waktu");
			$get_keterangan	= $this->input->post("keterangan");
			$jenis_gangguan	= $this->input->post("jenis_gangguan");
			
			$data = array(
				"JENIS_GANGGUAN" 	=> $jenis_gangguan,
				"TANGGAL" 			=> date('Y-m-d', strtotime($get_tanggal)),
				"WAKTU"				=> $get_waktu,
				"KETERANGAN"		=> $get_keterangan,
				"STATUS"			=> 2
			);
			
			if(!isset($id))
			{
				$this->m_history_trouble->save($data);
				redirect("history_trouble/grid_history_trouble");
			}
			else
			{
				$this->m_history_trouble->save($data,$id);
				redirect("history_trouble/grid_history_trouble");
			}
		}
	}
	public function tindak_lanjut_trouble($id = NULL)
	{
		$this->form_validation->set_rules("form_input","Form Proses","required");
		
		if($this->form_validation->run() == FALSE)
		{
			$data["id"] 				= $id;
			$data["menu"]				= "admin";
			$data["get_id_status"] 		= $this->m_history_trouble->select_data($id);
			$data["get_data_proses"] 	= $this->m_proses_tl_trouble->get_all($id);
			$data["post_action"] 		= "history_trouble/tindak_lanjut_trouble/".$id;
			$data["post_action2"] 		= "history_trouble/cek_status";
			$data["content"] 			= $this->load->view("form_tindak_lanjut_trouble",$data,true);
			$this->load->view("main",$data);
		}
		else
		{
			$get_id_pengaduan = $id;
			$get_datetime = date("Y-m-d H:i:s");
			$get_keterangan = $this->input->post("form_input");
			
			$data = array(
				"KD_TL_TROUBLE" => $get_id_pengaduan,
				"TANGGAL" => $get_datetime,
				"KETERANGAN_A" => $get_keterangan,
				"TESTIMONI" => 0
			);
			
			$this->m_proses_tl_trouble->save($data);
			echo "<script>alert('Data berhasil disimpan !');</script>";
			echo "<script>document.location='';</script>";
		}
	}
	
	public function cek_status()
	{
		$this->form_validation->set_rules("testimoni","Proses","required");
	
		$get_id = $this->input->post("id_testimoni");
		$get_datetime = date("Y-m-d H:i:s");
		$get_testimoni = $this->input->post("testimoni");
		
		$data = array(
			"KD_TL_TROUBLE" => $get_id,
			"TANGGAL" => $get_datetime,
			"KETERANGAN_A" => $get_testimoni,
			"TESTIMONI" => 1
		);
		$this->m_proses_tl_trouble->save($data);
		$data = array(
			"STATUS" => 1
		);
		$this->m_history_trouble->save($data,$get_id);
		redirect("history_trouble/tindak_lanjut_trouble/".$get_id);
	}
	
	public function data_report()
	{
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory');
	
		$this->form_validation->set_rules("tanggal_awal","Tangga Awal","required");
		$this->form_validation->set_rules("tanggal_akhir","Tangga Akhir","required");
		
		if($this->form_validation->run() == FALSE)
		{
			$data["menu"]			= "admin";
			$data["post_action"]	= "history_trouble/data_report";
			$data["content"]		= $this->load->view("form_data_report",$data,true);
			$this->load->view("main",$data);
		}
		else
		{
			$get_taw	= date('Y-m-d', strtotime($this->input->post("tanggal_awal")));
			$get_tak	= date('Y-m-d', strtotime($this->input->post("tanggal_akhir")));
			$get_data	= $this->m_history_trouble->get_tanggal($get_taw,$get_tak);
			
			$date = date('m');
			if($date == 1){
				$get_date = "Januari";
			}elseif($date == 2){
				$get_date = "Februari";
			}elseif($date == 3){
				$get_date = "Maret";
			}elseif($date == 4){
				$get_date = "April";
			}elseif($date == 5){
				$get_date = "Mei";
			}elseif($date == 6){
				$get_date = "Juni";
			}elseif($date == 7){
				$get_date = "Juli";
			}elseif($date == 8){
				$get_date = "Agustus";
			}elseif($date == 9){
				$get_date = "September";
			}elseif($date == 10){
				$get_date = "Oktober";
			}elseif($date == 11){
				$get_date = "November";
			}elseif($date == 12){
				$get_date = "Desember";
			}
			
			//buat data sheet baru
			$objPHPExcel = new PHPExcel();
			$objPHPExcel->getProperties()->setTitle("title")->setDescription("description");
			
			$objPHPExcel->setActiveSheetIndex(0);
			
			//merge untuk judul
			$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:E1');
			
			//mengatur lebar kolom jadi auto
			$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
			$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
			$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
			$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
			$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
			
			//mengatur teks align center
			$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			//mengatur border header tabel
			//$objPHPExcel->getActiveSheet()->getStyle('A4:E4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			
			//setting teks
			$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(24);
			$objPHPExcel->getActiveSheet()->getStyle('A4:E4')->getFont()->setBold(true);
				
			//judul
			$objPHPExcel->getActiveSheet()->setCellValue('A1', "Laporan Kinerja Kepala Sub Unit Program dan Informasi");
			
			//set bulan
			$objPHPExcel->getActiveSheet()->setCellValue('A3', "Tanggal Awal : ".$get_taw);
			$objPHPExcel->getActiveSheet()->setCellValue('B3', "Tanggal Akhir : ".$get_tak);
			
			//tabel header
			$objPHPExcel->getActiveSheet()->setCellValue('A4', "No");
			$objPHPExcel->getActiveSheet()->setCellValue('B4', "Tanggal");
			$objPHPExcel->getActiveSheet()->setCellValue('C4', "Jenis Gangguan");
			$objPHPExcel->getActiveSheet()->setCellValue('D4', "Keterangan");
			$objPHPExcel->getActiveSheet()->setCellValue('E4', "Penyelesaian Akhir");
			
			//isi kolom
			$no = 1;
			$numb = 5;
			foreach($get_data as $row){
				if($row["JENIS_GANGGUAN"] == 1){
					$set_jg = "Jaringan";
				}elseif($row["JENIS_GANGGUAN"] == 2){
					$set_jg = "Non Jaringan";
				}
				$objPHPExcel->getActiveSheet()->getStyle('A'.$numb)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('A'.$numb)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('A'.$numb)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('A'.$numb)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('B'.$numb)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('B'.$numb)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('B'.$numb)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('B'.$numb)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('C'.$numb)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('C'.$numb)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('C'.$numb)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('C'.$numb)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('D'.$numb)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('D'.$numb)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('D'.$numb)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('D'.$numb)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('E'.$numb)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('E'.$numb)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('E'.$numb)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				$objPHPExcel->getActiveSheet()->getStyle('E'.$numb)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
				
				$objPHPExcel->getActiveSheet()->setCellValue('A'.$numb, $no);
				$objPHPExcel->getActiveSheet()->setCellValue('B'.$numb, date('d-m-Y', strtotime($row["TANGGAL"])));
				$objPHPExcel->getActiveSheet()->setCellValue('C'.$numb, $set_jg);
				$objPHPExcel->getActiveSheet()->setCellValue('D'.$numb, $row["KETERANGAN"]);
				$objPHPExcel->getActiveSheet()->setCellValue('E'.$numb, $row["KETERANGAN_A"]);
				$numb++;
				$no++;
			}
			
			$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="Trouble History Report.xls"');
			header('Cache-Control: max-age=0');
			$objWriter->save('php://output');
			
		}
	}
}